package light.mvc.controller.wxn;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.pageModel.base.Grid;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.wxn.AccessTokenVO;
import light.mvc.service.wxn.AccessTokenServiceI;
import light.mvc.utils.BasisHttpRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/accessToken")
public class AccessTokenController extends BaseController {

	@Autowired
	private AccessTokenServiceI accessTokenService;

	@RequestMapping("/manager")
	public String manager() {
		return "/wxn/accessToken";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(AccessTokenVO accessToken, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(accessTokenService.dataGrid(accessToken, ph));
		grid.setTotal(accessTokenService.count(accessToken, ph));
		return grid;
	}

	@RequestMapping("/viewPage")
	public String viewPage(HttpServletRequest request, Long id) {
		AccessTokenVO r = accessTokenService.get(id);
		request.setAttribute("accessToken", r);
		return "/wxn/accessTokenView";
	}

	@RequestMapping("/addPage")
	public String addPage() {
		return "/wxn/accessTokenAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(AccessTokenVO accessToken) {
		Json j = new Json();
		try {
			accessTokenService.add(accessToken);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Long id) {
		Json j = new Json();
		try {
			accessTokenService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/get")
	@ResponseBody
	public AccessTokenVO get(Long id) {
		return accessTokenService.get(id);
	}

	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Long id) {
		AccessTokenVO r = accessTokenService.get(id);
		request.setAttribute("accessToken", r);
		return "/wxn/accessTokenEdit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(AccessTokenVO accessToken) {
		Json j = new Json();
		try {
			accessTokenService.edit(accessToken);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/scanAccessToken")
	@ResponseBody
	public Json scanAccessToken() {
		Json j = new Json();
		try {
			accessTokenService.scanFindByAppid();
			j.setSuccess(true);
			j.setMsg("获取成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/getAccessToken")
	@ResponseBody
	public Json getAccessToken(String appid) {
		Json j = new Json();
		try {
			AccessTokenVO accessToken = accessTokenService.appidToAccessToken(appid);
			//System.out.println("-"+accessToken.getAccess_Token());
			if(accessToken.getAccess_Token()!=null || accessToken.getAccess_Token()!="" || accessToken.getAccess_Token().length()>0){
				j.setSuccess(true);
				j.setMsg("获取成功！");
			}else{
				j.setSuccess(true);
				j.setMsg("获取失败！");
			}
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
}